<template>
  <div class="wrap">
    <el-dialog
      title="货品查找"
      :visible.sync="dialogVisible"
      width="65%"
      :showClose="false"
      :close-on-click-modal='false'
    >
      <!-- <span>这是一段信息</span> -->
      <span class="cancel" @click="goodsCancel" style="cursor:pointer;">X</span>
      <div class="goods-wrap">
        <el-form
          :inline="true"
          :model="dataForm"
          @keyup.enter.native="getDataList()"
        >
          <el-form-item>
            <div>货品分类：</div>
            <el-cascader
              ref="refSubCat"
              v-model="dataForm.categoryId"
              :options="options"
              placeholder="请选择"
              :props="{ checkStrictly: true, value: 'id', label: 'text' }"
              @change="handlechange"
               clearable
            ></el-cascader>
          </el-form-item>
          <el-form-item>
            <div>货品编码：</div>
            <el-input
              v-model="dataForm.barcode"
              placeholder="条码"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div>货品名称：</div>
            <el-input
              v-model="dataForm.name"
              placeholder="名称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div>可用量：</div>
            <el-input
              v-model="dataForm.specifications1"
              placeholder="最小可用量"
              clearable
            ></el-input
            >-<el-input
              v-model="dataForm.specifications2"
              placeholder="最大可用量"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div>现存量：</div>
            <el-input
              v-model="dataForm.model1"
              placeholder="最小现存量"
              clearable
            ></el-input
            >-
            <el-input
              v-model="dataForm.model2"
              placeholder="最大现存量"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div class="block"></div>
          </el-form-item>
          <el-form-item>
            <el-button @click="getDataList()">查询</el-button>
            <el-button @click="resetDataList()">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :data="dataList"
          border
          v-loading="dataListLoading"
          @row-click="singleElection"
          style="width: 100%"
        >
          <el-table-column label="" width="65">
            <template slot-scope="scope">
              <el-radio
                class="radio"
                v-model="templateSelection"
                :label="scope.$index"
                >&nbsp;</el-radio
              >
            </template>
          </el-table-column>
          <!-- <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50"
          >
          </el-table-column> -->
          <el-table-column
            prop="mBarCode"
            header-align="center"
            align="center"
            width="80"
            label="条码"
          >
          </el-table-column>
          <el-table-column
            prop="name"
            header-align="center"
            align="center"
            label="名称"
          >
          </el-table-column>
          <el-table-column
            prop="standard"
            header-align="center"
            align="center"
            label="规格"
          >
          </el-table-column>
          <el-table-column
            prop="model"
            header-align="center"
            align="center"
            width="180"
            label="型号"
          >
          </el-table-column>
          <el-table-column
            prop="categoryName"
            header-align="center"
            align="center"
            label="类别"
          >
          </el-table-column>
          <el-table-column
            prop="materialOther"
            header-align="center"
            align="center"
            label="扩展信息"
          >
          </el-table-column>
          <el-table-column
            prop="unitName"
            header-align="center"
            align="center"
            label="单位"
          >
          </el-table-column>
          <el-table-column
            prop="safetyStock"
            header-align="center"
            align="center"
            label="安全存量"
          >
          </el-table-column>
          <el-table-column
            prop="stock"
            header-align="center"
            align="center"
            label="库存"
          >
          </el-table-column>
        </el-table>
        <p class="btn">
          <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="goodsSure">确 定</el-button>
            <el-button @click="goodsCancel">取 消</el-button>
          </span>
        </p>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper"
          class="page"
        >
        </el-pagination>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
export default {
  props: ["isfind"], // 父组件给子组件传递值的时候就需要通过props去传递
  data() {
    return {
      dialogVisible: true,
      dataForm: {
        model1: "",
        model2: "",
        category: "",
        barcode: "",
        name: "",
        specifications1: "",
        specifications2: "",
        value: "",
      },
      dataList: [],
      options: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      categoryIds: "",
      templateSelection: "",
      goodsinfo: {},
    };
  },
  created() {
    this.getDataList();
    this.getCategory();
  },
  methods: {
    // 确定按钮
    goodsSure() {
      if (typeof this.templateSelection == "string") {
        this.$message({
          message: "请选中商品",
          type: "warning",
          duration: 1500,
        });
        return;
      }
      this.dialogVisible = false;
      console.log(this.goodsinfo);
      this.$emit("getGoods", this.goodsinfo);
      this.$emit("changeFind", this.dialogVisible);
    },
    // 取消按钮
    goodsCancel() {
      // console.log(this.isfind)
      // return
      this.dialogVisible = false;
      this.$emit("changeFind", this.dialogVisible);
    },
    // 选中当前行
    singleElection(row) {
      console.log(row);
      // return;
      this.goodsinfo = row;
      // this.templateRadio = row.id;
    },
    // 选中分类获取下拉状态
    handlechange() {
      console.log(this.$refs["refSubCat"].getCheckedNodes()[0].value);
      this.categoryIds = this.$refs["refSubCat"].getCheckedNodes()[0].value;
    },
    // 下拉
    checked(e) {
      if (e) {
        this.getCategory();
      }
    },
    // 选中
    change(id) {
      console.log(id);
      // this.dataForm.id = id;
    },
    // 获取分类的下拉框
    getCategory() {
      this.$http({
        url: this.$http.adornUrl(
          "/materialCategory/getMaterialCategoryTree?id="
        ),
        method: "post",
      }).then(({ data }) => {
        console.log("成功获取到菜单数据...", data.data);
        this.options = this.getTreeData(data.data);
      });
    },
    // 递归判断列表，把最后的children设为undefined
    getTreeData(data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].children.length < 1) {
          data[i].children = undefined;
        } else {
          this.getTreeData(data[i].children);
        }
      }
      return data;
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let str = `{"barCode":"${this.dataForm.barcode}","name":"${this.dataForm.name}","standard":"","endavailableStock":"${this.dataForm.specifications2}","beginavailableStock":"${this.dataForm.specifications1}","beginstock":"${this.dataForm.model1}","endstock":"${this.dataForm.model2}","model":"","categoryIds":"${this.categoryIds}","mpList":"规格,颜色,制造商"}`;
      var search = encodeURIComponent(str);
      this.$http({
        url: this.$http.adornUrl(`/material/list?search=${search}`),
        method: "get",
        params: this.$http.adornParams({
          currentPage: this.pageIndex,
          pageSize: this.pageSize,
        }),
      }).then(({ data }) => {
        //  console.log(data.data.list)
        if (data && data.code === 0) {
          this.dataList = data.data.list;
          this.totalPage = data.data.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 更新状态
    updateMemberStatus(data) {
      console.log("最新信息", data);
      let { id, status } = data;
      //发送请求修改状态
      this.$http({
        url: this.$http.adornUrl("/member/member/update/status"),
        method: "post",
        data: this.$http.adornData({ id, status }, false),
      }).then(({ data }) => {
        this.$message({
          type: "success",
          message: "状态更新成功",
        });
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 重置
    resetDataList() {
      // console.log(11)
      this.dataForm.model = "";
      this.dataForm.category = "";
      this.dataForm.barcode = "";
      this.dataForm.name = "";
      this.dataForm.specifications1 = "";
      this.dataForm.specifications2 = "";
      this.dataForm.model1 = "";
      this.dataForm.model2 = "";
      this.value1 = "";
      this.categoryIds = "";
      this.dataForm.categoryId = "";
      this.getDataList();
    },
    downLoadFiles(url, params) {
      return new Promise((resolve, reject) => {
        axios
          .post(this.$http.adornUrl(url), params, {
            responseType: "blob",
            headers: {
              "Content-Type": "application/json; application/octet-stream",
            },
          })
          .then((res) => {
            if (!res) {
              Message.warning("导出失败");
              return;
            }
            resolve(res);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
  },
};
</script>
<style scoped>
.el-form-item .el-form-item__content div {
  display: inline-block;
}
.el-input {
  position: relative;
  font-size: 0.072917rem;
  display: inline-block;
  width: 150px;
}
.cancel {
  position: absolute;
  right: 20px;
  top: 18px;
}
.wrap >>> .el-dialog__header {
  padding: 0.104167rem 0.104167rem 0.052083rem;
  background: #e6f5fe;
  text-align: center;
  font-size: 16px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  color: #31333d;
}
.wrap >>> .el-dialog__body {
  padding: 20px 40px;
}
.wrap >>> .has-gutter th {
  background: #dddddd;
}
.el-button {
  width: 80px;
  height: 32px;
}
.wrap .btn {
  text-align: right;
}
.page {
  text-align: center;
}
</style>
